package com.zbkj.admin.controller;

import cn.hutool.core.collection.CollUtil;
import com.zbkj.common.model.card.StoreGiftCard;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.StoreGiftCardRequest;
import com.zbkj.common.request.StoreGiftCardSearchRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.response.StoreGiftCardInfoResponse;
import com.zbkj.service.service.StoreGiftCardService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * 礼品卡模版管理控制器
 *
 * @author Swallows
 * @date 2022-08-18 14:35
 */
@Slf4j
@RestController
@RequestMapping("api/admin/marketing/giftCard")
@Api("营销 -- 礼品卡")
@RequiredArgsConstructor
public class StoreGiftCardController {

    private final StoreGiftCardService storeGiftCardService;

    @PreAuthorize("hasAuthority('admin:giftCard:list')")
    @GetMapping("/list")
    @ApiOperation("礼品卡模版分页列表")
    public CommonResult<CommonPage<StoreGiftCard>> getPageList(@Validated StoreGiftCardSearchRequest request, @Validated PageParamRequest pageParamRequest) {

        CommonPage<StoreGiftCard> storeGiftCardCommonPage = CommonPage.restPage(storeGiftCardService.getPageList(request, pageParamRequest));
        return CommonResult.success(storeGiftCardCommonPage);
    }

    @PostMapping("/save")
    @ApiOperation("新增礼品卡模版")
    @PreAuthorize("hasAuthority('admin:giftCard:save')")
    public CommonResult<String> createByRequest(@RequestBody @Validated StoreGiftCardRequest request) {

        if (storeGiftCardService.saveByRequest(request)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation("更新礼品卡模版状态")
    @PreAuthorize("hasAuthority('admin:giftCard:update')")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "礼品卡模版id", required = true),
            @ApiImplicitParam(value = "礼品卡模版状态", required = true)
    })
    public CommonResult<String> updateStatus(@RequestParam Integer id, @RequestParam Boolean status) {

        if (storeGiftCardService.updateStatusById(id, status)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('admin:giftCard:delete')")
    @ApiOperation("删除礼品卡模版信息")
    public CommonResult<String> deleteById(@ApiParam(value = "礼品卡模版id", required = true)
                                           @RequestParam Integer id) {

        if (storeGiftCardService.delById(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @GetMapping("/info")
    @ApiOperation("获取礼品卡模版详情")
    @PreAuthorize("hasAuthority('admin:giftCard:info')")
    public CommonResult<StoreGiftCardInfoResponse> getInfoById(@ApiParam(value = "礼品卡模版id", required = true)
                                                               @RequestParam Integer id) {

        return CommonResult.success(storeGiftCardService.getInfoById(id));
    }

    @PostMapping("/makeCards")
    @ApiOperation("制作实体卡")
    @PreAuthorize("hasAuthority('admin:giftCard:make')")
    public CommonResult<HashMap<String, String>> makeCards(@ApiParam(value = "礼品卡模版id", required = true) @RequestParam Integer id,
                                                           @ApiParam(value = "制作数量", required = true) @RequestParam Integer nums) {

        String fileName = storeGiftCardService.makeCard(id, nums);
        HashMap<String, String> map = CollUtil.newHashMap();
        map.put("fileName", fileName);
        return CommonResult.success(map);
    }
}
